(use-modules
 (gnu)
 (guix packages)
 (guix download)
 (guix git-download)
 ((guix licenses) #:prefix license:)
 (guix build-system)
 (guix build-system copy)
 (gnu packages mail)
 (gnu packages linux)
 (guix build-system trivial))

;; Kernel Configurations for Specific Machines

(define kconf-for-nuc
  (string-append (dirname (current-filename)) "/NUC.KCONF"))

(define-public linux-nuc-x15
  (package
   (inherit linux-libre)
   (name "linux-nuc-x15")
   (version "6.7.1-3")
   (source
    (origin
     (method url-fetch)
     (uri "https://cdn.kernel.org/pub/linux/kernel/v6.x/linux-6.7.1.tar.xz")
     (sha256
      (base32
       "1hv8mma3i6zhjix5k2g12jmajqy29c1xjfjkllmj18l6irbgmkqy"))))
   (native-inputs
    `(("kconfig", kconf-for-nuc)
      ,@(alist-delete "kconfig"
                      (package-native-inputs linux-libre))))
   (synopsis "Mainline Linux kernel for Intel NUC X15")
   (description "Mainline Linux kernel for Intel NUC X15")
   (license license:gpl2)
   (home-page "https://kernel.org")))

;; Custom Packages for Firmwares

(define (linux-firmware-version) "39adc8bd5bb8707db5982812e5170ffa3c4c9ec9")
(define (linux-firmware-source version)
  (origin
   (method git-fetch)
   (uri (git-reference
         (url "https://mirrors.tuna.tsinghua.edu.cn/git/linux-firmware.git")
         (commit version)))
   (file-name (string-append "linux-firmware-" version "-checkout"))
   (sha256
    (base32
     "1i472p82b1b1dxhcj3vdl8nccngk3m5iz3fhw1cl5vccvi948k59"))))

(define-public ibt-firmware-nonfree
  (package
   (name "ibt-firmware-nonfree")
   (version (linux-firmware-version))
   (source (linux-firmware-source version))
   (build-system trivial-build-system)
   (arguments
    `(#:modules ((guix build utils))
      #:builder (begin
                  (use-modules (guix build utils))
                  (let ((source (assoc-ref %build-inputs "source"))
                        (fw-dir (string-append %output "/lib/firmware/intel/")))
                    (mkdir-p fw-dir)
                    (for-each (lambda (file)
                                (copy-file file
                                           (string-append fw-dir (basename file))))
                              (find-files (string-append source "/intel/")
                                          ".ibt-*$"))
                    #t))))
   (home-page "https://git.kernel.org/cgit/linux/kernel/git/firmware/linux-firmware.git/tree/intel")
   (synopsis "Non-free firmware for Intel bluetooth devices")
   (description "Non-free intel bluetooth devices firmware")
   (license (license:non-copyleft
             "https://git.kernel.org/cgit/linux/kernel/git/firmware/linux-firmware.git/"))))

(define-public iwlwifi-firmware-nonfree
  (package
   (name "iwlwifi-firmware-nonfree")
   (version (linux-firmware-version))
   (source (linux-firmware-source version))
   (build-system trivial-build-system)
   (arguments
    `(#:modules ((guix build utils))
      #:builder (begin
                  (use-modules (guix build utils))
                  (let ((source (assoc-ref %build-inputs "source"))
                        (fw-dir (string-append %output "/lib/firmware/")))
                    (mkdir-p fw-dir)
                    (for-each (lambda (file)
                                (copy-file file
                                           (string-append fw-dir (basename file))))
                              (find-files source
                                          "iwlwifi-.*\\.ucode$|LICENSE\\.iwlwifi_firmware$"))
                    #t))))
   (home-page "https://wireless.wiki.kernel.org/en/users/drivers/iwlwifi")
   (synopsis "Non-free firmware for Intel wifi chips")
   (description "Non-free iwlwifi firmware")
   (license (license:non-copyleft
             "https://git.kernel.org/cgit/linux/kernel/git/firmware/linux-firmware.git/"))))

(define-public i915-firmware-nonfree
  (package
   (name "i195-firmware-nonfree")
   (version (linux-firmware-version))
   (source (linux-firmware-source version))
   (build-system trivial-build-system)
   (arguments
    `(#:modules ((guix build utils))
      #:builder (begin
                  (use-modules (guix build utils))
                  (let ((source (assoc-ref %build-inputs "source"))
                        (fw-dir (string-append %output "/lib/firmware/i915/")))
                    (mkdir-p fw-dir)
                    (for-each (lambda (file)
                                (copy-file file
                                           (string-append fw-dir (basename file))))
                              (find-files (string-append source "/i915/")
                                          ".*\\.bin$"))
                    #t))))
   (home-page "https://git.kernel.org/cgit/linux/kernel/git/firmware/linux-firmware.git/tree/i915")
   (synopsis "Non-free firmware for Intel GPUs")
   (description "Non-free i915 firmware")
   (license (license:non-copyleft
             "https://git.kernel.org/cgit/linux/kernel/git/firmware/linux-firmware.git/"))))
